JavaScript provides a Date object that can be used to display date and time on the web pages according to the browser time zone. In this JavaScript tutorial, we will learn how to work with JS Date objects.
JavaScript Date
To represent Date data in JavaScript we generally use the built-in date object, which can output the current date according to the browser date time zone.
Syntax
obj_name = new Date();
Example
<script>
today = new Date();
console.log(today);
</script>
Output
Mon Mar 22 2021 13:50:58 GMT+0530 (India Standard Time)
To create a date object we use the new keyword followed by the Date() object. By default, the Date() object to print the current date & time in the following format
Day Month Date Year Hours:Minutes:Seconds GMT Standared Time Zone
Create Date Object
We can create different Date objects with the help of
new Date()
constructor. And there are four different syntaxes we can follow to create a date-time object.
Syntax
new Date(); // for current date and time
new Date(year, month, day, hours, minutes, seconds, milliseconds); // for manual date
new Date(milliseconds); // date using milliseconds timestamps
new Date(string date); // create date object using string
Now let's discuss all these 4 different syntaxes to create date objects.
new Date()
The
new Date()
statement is used to create a date object of the current time.
Example
<script>
current= new Date();
console.log(current); //Mon Mar 22 2021 14:02:35 GMT+0530 (India Standard Time)
</script>
new Date(year, month, date, hours, ...)
We can also create a specific date and time object using the new Date() constructor by specifying the 7 parameters.
Syntax
date_obj_name = new Date(year, month, date, hours, minutes, seconds, millisecond)
Example
<script>
my_dob = new Date(1999, 08,03,12,33,0,0)
console.log(my_dob)
</script>
Output
Fri Sep 03 1999 12:33:00 GMT+0530 (India Standard Time)
JavaScript count Months from 0 to 11, parameter value 0 represents Jan, and 11 represents Dec. All the Date parameters values are optional, and by default, their value is maximized by their time limit. We can also create a manual date object by only specifying the date and month parameters.
Example
<script>
my_dob_date = new Date(1999, 08, 03);
my_dob_month = new Date(1999, 08);
console.log(my_dob_date); //Fri Sep 03 1999 00:00:00 GMT+0530 (India Standard Time)
console.log(my_dob_month) //Wed Sep 01 1999 00:00:00 GMT+0530 (India Standard Time)
</script>
We can not omit the month parameter values when creating a manual date object with years and month, else the Date object will treat the single year value as milliseconds that we will cover in the further section.
new Date(milliseconds)
We can also create a date from milliseconds, where the 0 milliseconds represent the Following date time.
January 01, 1970 00:00:00 UTC
Example
<script>
initial_date = new Date(0);
current_date = new Date(1616403127169);
console.log(initial_date); //Thu Jan 01 1970 05:30:00 GMT+0530 (India Standard Time)
console.log(current_date); //Mon Mar 22 2021 14:22:07 GMT+0530 (India Standard Time)
</script>
The reason why we can not pass only a single year parameter value to the Date object is becaue it will treat the only year value as milliseconds.
new Date(string date)
We can also use the string representation to convert string Date data to a proper JS date object.
Example
<script>
today = new Date("March 22, 2021");
tomorrow_alarm = new Date("March 23, 2021 11:20:00");
console.log(today);
console.log(tomorrow_alarm);
</script>
Output
Mon Mar 22 2021 00:00:00 GMT+0530 (India Standard Time)
Tue Mar 23 2021 11:20:00 GMT+0530 (India Standard Time)
Summary
-
To create a new date object we can use the
new Date()
constructor. - If we do not specify any parameter value to the Date() object it returns the current date and time.
- We can either use the manual date, milliseconds, or string date format to create a new manual date object.
People are also reading: